package com.example.mq.mqserver.dataCenter;

import com.example.mq.DemoApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;


import java.io.File;
import java.util.List;

/**
 * 数据库数据管理
 */
public class DataBaseManager {
    //为避免因 metaMapper 造成的空指针异常 需要获取到Spring中的 metaMapper 对象
    //但想注入 metaMapper 对象,就要求 DataBaseManger 也添加了注解被Spring管理
    //但此时我们想要 DataBaseManger 对象受自己管理
    private MetaMapper metaMapper;

    //数据库的初始化操作
    public void init(){
        //通过这种方法 获取到程序中的 metaMapper 对象
        metaMapper = DemoApplication.context.getBean(MetaMapper.class);
        //如果不存在数据库 --- 建库建表
        if(!checkDBExits()){
            //创建 data 目录
            File file = new File("./data");
            file.mkdirs();
            //创建数据表
            createTable();

            //插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManger] 完成数据库创建.初始化成功");
        }else
            System.out.println("[DataBaseManger] 数据库已存在.");
    }



    //检查是否存在数据库
    private boolean checkDBExits() {
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }

    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret){
            System.out.println("[DataBaseManger] 删除数据库文件成功");
        }else
            System.out.println("[DataBaseManger] 删除数据库文件失败");

        //删除数据库目录
        File dataDir = new File("./data");
        ret = dataDir.delete();
        if(ret){
            System.out.println("[DataBaseManger] 删除数据库目录成功");
        }else
            System.out.println("[DataBaseManger] 删除数据库目录失败");
    }

    //这里只创建数据表
    //MyBatis 在程序启动期间会帮助我们创建 meta.db 文件
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManger] 创建表成功");
    }

    //创建默认的交换机
    private void createDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        insertExchange(exchange);
        System.out.println("[DataBaseManger] 默认交换机创建成功");
    }

    //封装 交换机 队列 绑定关系 的数据库操作

    //封装交换机
    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }

    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }

    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }

    //封装队列
    public void insertQueue(MSGQueue msgQueue){
        metaMapper.insertQueue(msgQueue);
    }

    public List<MSGQueue> selectAllMSGQueues(){
        return metaMapper.selectAllMSGQueues();
    }

    public void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }

    //封装绑定关系
    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }

    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }


}
